package com.yq.controller;

import java.io.File;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.yq.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yq.entity.Banner;
import com.yq.service.BannerService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class BannerCtrl {
	@Autowired
	private BannerService bannerService;
	private Banner banner= new Banner();
	Map<String, Object> map = new HashMap<String, Object>();
	
	
	@RequestMapping(value = "/main/banAddjsp.html")
	public ModelAndView addjsp() {
		return new ModelAndView("main/banner/add");
	}
	
	@ResponseBody
	@RequestMapping(value = "/main/banInsert.html")
	public String insert(@RequestParam(value="file") MultipartFile file, HttpServletRequest request, Banner banner) {
		/*	map.put("url", url);
			map.put("type", type);
			map.put("ban_img", ban_img);
			map.put("status", 1);
			map.put("sort", 0);*/
		if(!file.isEmpty()){
			String realpath = request.getSession().getServletContext().getRealPath("");
			String path = "";
			if(realpath.contains("\\")){
				path = realpath.substring(0,realpath.lastIndexOf("\\"))+"/upload";
			}else{
				path = realpath.substring(0,realpath.lastIndexOf("/"))+"/upload";
			}
//			System.out.println("cate="+cate);
//			System.out.println("path="+path);
//			String fileName = file.getOriginalFilename();
			String fileName = new Date().getTime()+".png";
//			cate.setCtg_name(URLDecoder.decode(cate.getCtg_name(),"utf-8"));

			File targetFile = new File(path, fileName);
			if(!targetFile.exists()){
				targetFile.mkdirs();
			}

			//保存
			try {
				file.transferTo(targetFile);
				banner.setBan_img(fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		banner.setSort(0);
		banner.setStatus(1);
		return bannerService.insert(banner)+"";
	}
	@ResponseBody
	@RequestMapping(value = "/main/banUpdate.html")
	public Object update(@RequestParam(value="file") MultipartFile file, HttpServletRequest request, Banner banner) {
/*		map.put("url", url);
		map.put("ban_img", ban_img);
		map.put("ban_id", ban_id);*/
		if(!file.isEmpty()){
			String realpath = request.getSession().getServletContext().getRealPath("");
			String path = "";
			if(realpath.contains("\\")){
				path = realpath.substring(0,realpath.lastIndexOf("\\"))+"/upload";
			}else{
				path = realpath.substring(0,realpath.lastIndexOf("/"))+"/upload";
			}
//			System.out.println("cate="+cate);
//			System.out.println("path="+path);
//			String fileName = file.getOriginalFilename();
			String fileName = new Date().getTime()+".png";
//			cate.setCtg_name(URLDecoder.decode(cate.getCtg_name(),"utf-8"));

			File targetFile = new File(path, fileName);
			if(!targetFile.exists()){
				targetFile.mkdirs();
			}

			//保存
			try {
				file.transferTo(targetFile);
				banner.setBan_img(fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bannerService.update(banner)+"";
		
	}
	@ResponseBody
	@RequestMapping(value = "/main/banUpstatus.html")
	public Object upstatus(Integer ban_id,Integer status) {
		map.put("status", status);
		map.put("ban_id", ban_id);
		return bannerService.upstatus(map)+"";
	}
	
	@ResponseBody
	@RequestMapping(value = "/main/banSort.html")
	public Object sort(Integer ban_id,Integer sort) {
		map.put("sort", sort);
		map.put("ban_id", ban_id);
		return bannerService.sort(map)+"";
	}
	
	@RequestMapping(value = "/main/banList.html")
	public ModelAndView list(Integer status) {
		banner.setStatus(status);
		List<Banner> list = bannerService.list(banner);
		ModelAndView ml = new ModelAndView();
		ml.addObject("list", list);
		ml.setViewName("main/banner/list");
		return ml;
	}
	@RequestMapping(value = "/main/banListById.html")
	public ModelAndView listById(Integer ban_id) {
		banner.setBan_id(ban_id);
		List<Banner> list = bannerService.listById(banner);
		ModelAndView ml = new ModelAndView();
		ml.addObject("bannerlist", list);
		ml.setViewName("main/banner/info");
		return ml;
	}
}
